﻿using FaceAppClient.FaceModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Baidu.Aip.Face;

namespace FaceAppClient.Baidu.FaceAPI
{
    public class FaceOperation
    {
        public static string imagePath;
        public static string base64;
        public static string token = AccessToken.getAccessToken();
        // 人脸探测
        public static string FaceDetect()
        {            
            string host = "https://aip.baidubce.com/rest/2.0/face/v1/detect?access_token=" + token;
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.Method = "post";
            request.KeepAlive = true;
            // 图片的base64编码
            //string base64 = FileUtils.getFileBase64(ImagePath);
            String str = "max_face_num=" + 5 + "&face_fields=" + "age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities" + "&image=" + HttpUtility.UrlEncode(base64);
            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string result = reader.ReadToEnd();
            //Console.WriteLine("人脸探测:");
            //Console.WriteLine(result);
            return result;
        }

        public static string FaceIdentify()
        {
            //string token = AccessToken.getAccessToken();
            string host = "https://aip.baidubce.com/rest/2.0/face/v2/identify?access_token=" + token;
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.Method = "post";
            request.KeepAlive = true;
            //string base64 = FileUtils.getFileBase64(ImagePath);
            String str = "group_id=" + "group1" + "&user_top_num=" + "1" + "&face_top_num=" + "1" + "&images=" + HttpUtility.UrlEncode(base64);
            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string result = reader.ReadToEnd();            
            return result;            
        }

        public static string FaceAdd()
        {
            //string token = AccessToken.getAccessToken();
            string host = "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add?access_token=" + token;
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.Method = "post";
            request.KeepAlive = true;
            //string base64 = FileUtils.getFileBase64(ImagePath);
            //String str = "uid=" + "test_user_5" + "&user_info=" + "userInfo5" + "&group_id=" + "test_group_2" + "&images=" + HttpUtility.UrlEncode(base64);
            Random rd = new Random();
            string uid = rd.Next(100000,999999).ToString();
            String str = string.Format("uid={0}&user_info={1}&group_id={2}&images={3}", uid, Path.GetFileNameWithoutExtension(imagePath), "group1", HttpUtility.UrlEncode(base64));
            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string result = reader.ReadToEnd();
            //Console.WriteLine("人脸注册:");
            //Console.WriteLine(result);
            return result;
        }
               

        public static int CheckFace(FaceModel.DetectModel fd)
        {
            dResult result = fd.result[0];
            Occlusion occlusion = result.qualities.occlusion;
            double left_eye = occlusion.left_eye;
            double right_eye = occlusion.right_eye;
            double nose = occlusion.nose;
            double mouth = occlusion.mouth;
            double left_check = occlusion.left_check;
            double right_check = occlusion.right_check;
            double chin_contour = occlusion.chin;
            if (left_eye > 0.6)
                return 1;
            if (right_eye > 0.6)
                return 2;
            if (nose > 0.7)
                return 3;
            if (mouth > 0.7)
                return 4;
            if (left_check > 0.8)
                return 5;
            if (right_check > 0.8)
                return 6;
            if (chin_contour > 0.6)
                return 7;

            double blur = result.qualities.blur;
            if (blur > 0.7)
                return 8;

            //double illumination = result.qualities.illumination;
            //if (illumination < 40)
            //    return 9;

            double pitch = result.pitch;
            double roll = result.roll;
            double yaw = result.yaw;
            if (Math.Abs(pitch) > 20 || Math.Abs(roll) > 20 || Math.Abs(yaw) > 20)
                return 9;
            //double completeness = result.qualities.completeness;

            uint width = result.location.width;
            uint height = result.location.height;
            if (width < 100 || height < 100)
                return 10;
            return 0;
        }
    }
}
